package com.ruoyi.web.controller.tag;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.druid.util.StringUtils;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oos.tag.domain.TagConfig;
import com.ruoyi.oos.tag.domain.TagLimitdtl;
import com.ruoyi.oos.tag.mapper.TagLimitdtlMapper;
import com.ruoyi.oos.tag.service.ITagConfigService;
import com.ruoyi.oos.tag.service.ITagGroupService;
import com.ruoyi.oos.tag.service.impl.PiUtilBean;

import cn.hz.pi.api.PiApiUtil;
import cn.hz.pi.api.util.DateUtil;
import cn.hz.pi.api.util.TagValue;

/**
 * 测点配置Controller
 * 
 * @author wzy
 * @date 2020-05-11
 */
@Controller
@RequestMapping("/tag/config")
public class TagConfigController extends BaseController
{
    private String prefix = "tag/config";

    @Autowired
    private ITagConfigService tagConfigService;
    
    @Autowired
    private TagLimitdtlMapper limitMapper;
    
    @Autowired
    private ITagGroupService tagGroupService;

   
    @GetMapping()
    public String config()
    {
        return prefix + "/config";
    }

        /**
     * 查询测点配置列表
     */
    
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TagConfig tagConfig)
    {
    	if (tagConfig.getGroupid() != null) {
    		List<Long> list = tagGroupService.selectSubTagGroupById(tagConfig.getGroupid());
    		tagConfig.setGroupIds(list);
    	}
    	
      startPage();
      List<TagConfig> list = tagConfigService.selectTagConfigList(tagConfig);
      return getDataTable(list);
    }
    
    /**
     * 导出测点配置列表
     */
    @Log(title = "测点配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TagConfig tagConfig)
    {
        List<TagConfig> list = tagConfigService.selectTagConfigList(tagConfig);
        ExcelUtil<TagConfig> util = new ExcelUtil<TagConfig>(TagConfig.class);
        return util.exportExcel(list, "config");
    }

    /**
     * 新增测点配置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存测点配置
     */
    @Log(title = "测点配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TagConfig tagConfig)
    {
        return toAjax(tagConfigService.insertTagConfig(tagConfig));
    }

    /**
     * 修改测点配置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TagConfig tagConfig = tagConfigService.selectTagConfigById(id);
        mmap.put("tagConfig", tagConfig);
        return prefix + "/edit";
    }
    
    @GetMapping("/echart/{id}")
    public String echart(@PathVariable("id") Long id, ModelMap mmap) throws Exception
    {
    	TagConfig tagConfig = tagConfigService.selectTagConfigById(id);//查询测点
    	PiUtilBean brean = tagConfigService.getChartData(tagConfig.getPitagname(), DateUtil.addDay(DateUtil.getTime(), -1), DateUtil.getTime());////查询测点曲线数据
        List<Float> list =brean.getRetrunList();
        mmap.put("tagConfig", tagConfig.getTagname()+"("+DateUtil.addDay(DateUtil.getTime(), -1)+"至"+DateUtil.getTime()+")");
        mmap.put("list", list);
        mmap.put("xList",brean.getReturntimes());
        
        TagLimitdtl limtitl = new TagLimitdtl();
        limtitl.setConfigid(id);
       List<TagLimitdtl> limittlList =  limitMapper.selectTagLimitdtlList(limtitl);
        Float i = 2f;
       if(limittlList!= null && list.size()>0) {
    	   List<ChartBean> limitList = new ArrayList<ChartBean>();
    	   for (TagLimitdtl tagLimitdtl : limittlList) {
    		   ChartBean bean = new ChartBean();
    		   Float limitvalue=  tagLimitdtl.getLimitval();
    		   if(limitvalue == null) {
    			   limitvalue =0f;
    		   }
    		   bean.setyAxis(limitvalue);
    		   limitList.add(bean);
		}
    	   mmap.put("limitList", limitList); 
       }
       
        return prefix + "/echartData";
    }
    

    /**
     * 修改保存测点配置
     */
    @Log(title = "测点配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TagConfig tagConfig)
    {
        return toAjax(tagConfigService.updateTagConfig(tagConfig));
    }

    /**
     * 删除测点配置
     */
    @Log(title = "测点配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tagConfigService.deleteTagConfigByIds(ids));
    }
    

    /**
     * 选择测点树
     */
    @GetMapping("/selectTagTree/{tagid}")
    public String selectTagTree(@PathVariable("tagid") Long tagid, ModelMap mmap)
    {
    		TagConfig tagConfig = null;
    		
    		if (tagid != null && tagid != 0) {
    			tagConfig = tagConfigService.selectTagConfigById(tagid);
    		}
  			if (tagConfig == null) {
  				tagConfig = new TagConfig();
  			}
    		mmap.put("data", tagConfig);
        
        return prefix + "/tree";
    }

    /**
     * 加载测点列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = tagConfigService.selectTagTree();
        return ztrees;
    }

    /**
            *          测点导入
     */
    @GetMapping("/importTagName")
    public String importTagName() {
   
    	 return prefix + "/importTagName";
    }
    
    @PostMapping("/importTagNameJson")
    @ResponseBody
    public String importTagNameJson() {
    	System.out.println("importTagName");
    	return "success";
    }
    
    /**
     * 导入数据
     */
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TagConfig> util = new ExcelUtil<TagConfig>(TagConfig.class);
        List<TagConfig> userList = util.importExcel(file.getInputStream());
        List<TagConfig> sqlconfigList = tagConfigService.selectTagConfigList(null);
        Map<String,String> map = new HashMap<String,String>();
        for (TagConfig tagConfig : sqlconfigList) {
        	map.put(tagConfig.getPitagname(), tagConfig.getPitagname());
		}
        for (TagConfig tagConfig : userList) {
		    String tagName = tagConfig.getPitagname();
		    if(!map.containsKey(tagName)) {
		    	tagConfigService.insertTagConfig(tagConfig);
		    }
		}
       // String message = importUser(userList, updateSupport);
        return AjaxResult.success("导入成功!");
    }
    
    @GetMapping("/tagDetail")
    public String tagDetail(ModelMap model)
    {
      String  pitagname = "CDM158";
      String  startTime = DateUtils.getDate(DateUtils.addDays(new Date(), -1));
      String   endTime=DateUtils.getDate()+ " " + DateUtils.getDateHHMMSSStr(new Date()) ;
      model.put("pitagname", pitagname);
      model.put("startTime", startTime);
      model.put("endTime", endTime);
        return prefix + "/tagDetail";
    }
    
    @GetMapping("/getTagDetail")
    @ResponseBody
    public Map<String,Object> getTagDetail(TagConfig tagConfig,String startTime,String endTime){
    	
    	Map<String,Object> returnMap = new HashMap<String,Object>();
    	
    	if(StringUtils.isEmpty(startTime)) {
    		  startTime = DateUtils.getDate(DateUtils.addDays(new Date(), -1));
    	}
    	
    	if(StringUtils.isEmpty(endTime)) {
    		endTime=DateUtils.getDate() ;
    	}
    	startTime =startTime +"  00:00:00";
    	endTime =endTime+" " + DateUtils.getDateHHMMSSStr(new Date());
    	String pitagname =tagConfig.getPitagname();
    	
    	if(pitagname == null) {
    		pitagname = "CDM158";
    	}
    	List<TagValue> list =  null;
    	List<String> xdata = new ArrayList<String>();
    	List<Float> ydata =  new ArrayList<Float>();
    	if (pitagname != null) {
    		list = PiApiUtil.getInfoList(null, pitagname, startTime, endTime, 30);//总时间);
    		returnMap.put("listdata",list);
    		for (TagValue tagValue : list) {
    			ydata.add(tagValue.getValue());
    			xdata.add(tagValue.getTime());
   		     }
	    }
    	List<Map<String,Object>> xlist = new ArrayList<Map<String,Object>>();
    	Map<String,Object>  map =new HashMap<>();
    	map =new HashMap<>();
    	map.put("title", "时间");
    	map.put("field", "time");
    	xlist.add(map);
    	map =new HashMap<>();
    	map.put("title", "值");
    	map.put("field", "value");
    	xlist.add(map);
    	map =new HashMap<>();
    	map.put("title", "状态");
    	map.put("field", "state");
    	xlist.add(map);
    	
    	returnMap.put("xdata", xlist);
    	
    	String title = startTime +"--"+endTime;
    	returnMap.put("title", title);
    	returnMap.put("xchart", xdata);
    	returnMap.put("ychart", ydata);
    	  return returnMap;
    }
    
    

}
